<template>
	<view>
		<view @click="toSearch">
			<u-search placeholder="请输入关键词" border-color="#1296db"></u-search>
		</view>
		<u-swiper :list="bannerList" height="320"></u-swiper>
		<view class="u-font-39 u-text-center u-p-t-20" style="font-weight: 600;">新品推荐</view>
		<template v-if="username">
			<u-row gutter="16">
				<u-col span="6" v-for="good in goodsList">
					<goods-card :good="good"></goods-card>
				</u-col>
			</u-row>
		</template>

		<template v-else>
			<u-empty text="暂无权限" mode="permission"></u-empty>
			<view class="u-flex u-row-center" style="color: #1296db;" @click="toLogin">点击去登录</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bannerList: [],
				goodsList: [],
				username: ''
			}

		},
		onLoad() {
			this.getBannerList()
		},
		onShow() {
			this.getGoodsList()
			try {
				const username = uni.getStorageSync('username');
				if (username) {
					this.username = username
				}
			} catch (e) {
				// error
			}
		},
		methods: {
			//获取轮播图数据
			async getBannerList() {
				uniCloud.callFunction({
					name: 'banner'
				}).then(res => {
					this.bannerList = res.result.data
				})
			},
			//获取商品列表数据
			getGoodsList() {
				const username = uni.getStorageSync('username');
				uniCloud.callFunction({
					name: "listGoods",
					data: {
						username: username
					}
				}).then(res => {
					// console.log(res);
					this.goodsList = res.result.data
					// console.log(this.goodsList)
				})
			},
			toSearch() {
				this.$u.route('/pages/search/search');
			},
			toLogin() {
				this.$u.route({
					type: 'reLaunch',
					url: 'pages/user/login'
				})
			}

		},


	}
</script>

<style lang="scss" scoped>

</style>
